<template>
  <el-dialog title="关联权限" :visible.sync="dialogShow">
    <el-tree
      :data="allAuth"
      :check-strictly="true"
      :props="{label:'name'}"
      node-key="id"
      :default-checked-keys="defaultCheck"
      show-checkbox
    />
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="dialogShow">取消</el-button>

    </el-row>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { roleDetail, setRoleAuth } from '@/api/settings'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'SetAuth',
  data() {
    return {
      dialogShow: false,
      allAuth: [], // 所有权限
      defaultCheck: [], // 选中的节点
      roleId: ''
    }
  },
  methods: {
    // 获取权限
    async getAllAuth() {
      const res = await getPermissionList()
      this.allAuth = tranListToTreeData(res, '0')
    },
    // 获取当前角色信息
    async getRoleInfo(id) {
      this.roleId = id
      const res = await roleDetail(id)
      this.defaultCheck = res.permIds
    },
    // 修改提交
    async onSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await setRoleAuth({
        id: this.roleId,
        permIds
      })
      // 提示成功
      this.$message.success('关联成功')
      // 关闭
      this.dialogShow = false
    }

  }
}
</script>

<style>

</style>
